<?php

/**
 * 文章管理
 * @author Administrator
 */

namespace Operation\Controller;

use Operation\Model\Goods;
use Think\Controller;
use Think\IReq;
use Think\IFilter;
use Think\JSON;
use Think\Page;
use Think\ITime;

class ArticleController extends ComController {

    //[文章]
    function article_list(){
        $p = IFilter::act(IReq::get('p'), 'int') ? IFilter::act(IReq::get('p'), 'int') : 1;

        $goodsHandle = M('article as ar');
        $join = 'left join sm_article_category as ac on ac.id = ar.category_id';
        $order = 'ar.sort asc,ar.id desc';
        $page = '15';
        $field = 'ar.id,ar.title,ar.create_time,ar.top,ar.style,ar.color,ar.sort,ar.visibility,ac.name';
        $where = '';
        
        $articles = $goodsHandle->where($where)->join($join)->order($order)->field($field)->page($p . ",$page")->select();
        
        $count = $goodsHandle->where($where)->join($join)->count();

        $Page = new Page($count, $page);
        //dump($Page->parameter);die;
        $show = $Page->show($param);

        $this->assign('page', $show);
        $this->assign('articles',$articles);
        $this->display();
    }


    //[文章]删除
    function article_del() {
        $id = IFilter::act(IReq::get('id'), 'int');
        if (!empty($id)) {
            $obj = new IModel('article');
            $relationObj = new IModel('relation');

            if (is_array($id) && isset($id[0]) && $id[0] != '') {
                $id_str = join(',', $id);
                $where1 = ' id in (' . $id_str . ')';
                $where2 = ' article_id in (' . $id_str . ')';
            } else {
                $where1 = 'id = ' . $id;
                $where2 = 'article_id = ' . $id;
            }
            $obj->del($where1);               //删除商品
            $relationObj->del($where2);       //删除关联商品表
            $this->redirect('article_list');
        } else {
            $this->redirect('article_list', false);
            Util::showMessage('请选择要删除的文章');
        }
    }

    //[文章]单页
    function article_edit() {
        $id = IFilter::act(IReq::get('id'), 'int');
        if ($id) {
            //获取文章信息
            $articleObj = M('article');
            $articleRow = $articleObj->find($id);
            $articleRow = $articleRow ? $articleRow : $this->articleRow;
            if (!$articleRow) {
                IError::show(403, "文章信息不存在");
            }
            $articleRow['content'] = stripslashes($articleRow['content']);
            $this->assign('articleRow', $articleRow);
            $this->assign('json_articleRow', JSON::encode($articleRow));
        }
        //dump($articleRow);die;
        $catObj = M('article_category');
        $article_category = $catObj->order('path asc')->select();
        foreach ($article_category as $key => $value) {
            $article_category[$key]['path'] = str_repeat('&nbsp;&nbsp;&nbsp;',substr_count($value['path'],',')-2);
        }
        $this->assign('article_category', $article_category);
        
        $this->display();
    }

    //[文章]增加修改
    function article_edit_act() {
        $id = IFilter::act(IReq::get('id'), 'int');

        $articleObj = M('article');
        $dataArray = array(
            'title' => IFilter::act(IReq::get('title', 'post')),
            'content' => IFilter::act(IReq::get('content', 'post'), 'text'),
            'category_id' => IFilter::act(IReq::get('category_id', 'post'), 'int'),
            'create_time' => ITime::getDateTime(),
            'keywords' => IFilter::act(IReq::get('keywords', 'post')),
            'description' => IFilter::act(IReq::get('description', 'post')),
            'visibility' => IFilter::act(IReq::get('visibility', 'post'), 'int'),
            'top' => IFilter::act(IReq::get('top', 'post'), 'int'),
            'sort' => IFilter::act(IReq::get('sort', 'post'), 'int'),
            'style' => IFilter::act(IReq::get('style', 'post')),
            'color' => IFilter::act(IReq::get('color', 'post')),
        );

        //检查catid是否为空
        if ($dataArray['category_id'] == 0) {
            $this->articleRow = $dataArray;
            $this->redirect('article_edit', false);
            Util::showMessage('请选择分类');
        }

        if ($id) {
            //开始更新操作
            $dataArray['id'] = $id;
            $is_success = $articleObj->save($dataArray);
        } else {
            $id = $articleObj->add($dataArray);
            $is_success = $id ? true : false;
        }

        if ($is_success) {
            $this->redirect('article_list');
        } else {
            $this->articleRow = $dataArray;
            $this->redirect('article_edit', false);
            Util::showMessage('插入数据时发生错误');
        } 
    }

    //[文章分类]
    function article_cat_list(){
        $catObj = M('article_category');
        $order = 'path';
        $article_category = $catObj->order($order)->select();
        foreach ($article_category as $key => $value) {
            $article_category[$key]['path'] = (substr_count($value['path'],',')-2)*20;
        }
        //dump($article_category);die;
        $this->assign('article_category', $article_category);
        $this->display();
    }
    
    //[文章分类] 增加和修改动作
    function cat_edit_act() {
        $id = IFilter::act(IReq::get('id', 'post'));
        $parent_id = IFilter::act(IReq::get('parent_id', 'post'));

        $catObj = M('article_category');
        $DataArray = array(
            'parent_id' => $parent_id,
            'name' => IFilter::act(IReq::get('name', 'post'), 'string'),
            'issys' => IFilter::act(IReq::get('issys', 'post')),
            'sort' => IFilter::act(IReq::get('sort', 'post')),
        );

        /* 开始--获取path信息 */
        //1,修改操作
        if ($id) {
            $catRow = $catObj->find($id);
            if ($catRow['parent_id'] == $parent_id) {
                $isMoveNode = false;
                $DataArray['path'] = $catRow['path'];
            }
            else
                $isMoveNode = true;

            $localId = $id;
        }
        //2,新增操作
        else {
            $max_id = $catObj->field('max(id) as max_id')->find();
            $localId = $max_id['max_id'] ? $max_id['max_id'] + 1 : 1;
        }

        //如果不存在path数据时,计算path数据
        if (!isset($DataArray['path'])) {
            //获取父节点的path路径
            if ($parent_id == 0)
                $DataArray['path'] = ',' . $localId . ',';
            else {
                $parentRow = $catObj->find($parent_id);
                $DataArray['path'] = $parentRow['path'] . $localId . ',';
            }
        }
        /* 结束--获取path信息 */

        //1,修改操作
        if ($id) {
            //节点移动
            if ($isMoveNode == true) {
                if ($parentRow['path'] != null && strpos($parentRow['path'], ',' . $id . ',') !== false) {
                    $this->catRow = array(
                        'parent_id' => $DataArray['parent_id'],
                        'name' => $DataArray['name'],
                        'issys' => $DataArray['issys'],
                        'sort' => $DataArray['sort'],
                        'id' => $id,
                    );
                    $this->redirect('article_cat_edit', false);
                    Util::showMessage('不能该节点移动到其子节点的位置上');
                } else {
                    //其子节点批量移动
                    $childObj = M('article_category');
                    $oldPath = $catRow['path'];
                    $newPath = $DataArray['path'];

                    $where = 'path like "' . $oldPath . '%"';
                    $updateData = array(
                        'path' => "replace(path,'" . $oldPath . "','" . $newPath . "')",
                    );
                    $childObj->where($where)->save($updateData);
                }
            }
            $childObj['id'] = $id;
            $catObj->save($DataArray);
        }
        //2,新增操作
        else
            $catObj->add($DataArray);

        $this->redirect('article_cat_list');
    }

    //[文章分类] 增加修改单页
    function article_cat_edit() {
        $data = array();
        $id = IFilter::act(IReq::get('id'), 'int');

        $catObj = M('article_category');
        if ($id) {
            $data = $catObj->find($id);
            $this->assign('catRow', $data);
            $this->assign('json_catRow',  JSON::encode($data));
        }
        
        $article_category = $catObj->where(array('id != '.$id))->order('path asc')->select();
        foreach ($article_category as $key => $value) {
            $article_category[$key]['path'] = str_repeat('&nbsp;&nbsp;&nbsp;',substr_count($value['path'],',')-2);
        }
        $this->assign('article_category', $article_category);
        
        $this->display();
    }

    //[文章分类] 删除
    function cat_del() {
        $id = IFilter::act(IReq::get('id'), 'int');
        $catObj = M('article_category');

        //是否执行删除检测值
        $isCheck = true;

        //检测是否有parent_id 为 $id
        $where = array('parent_id' => $id);
        $catData = $catObj->where($where)->find();
        if (!empty($catData)) {
            $isCheck = false;
            $message = '此分类下还有子分类';
        }

        //检测是否有article的category_id 为 $id
        else {
            $articleObj = M('article');
            $where = array('category_id' => $id);
            $catData = $articleObj->where($where)->find();

            if (!empty($catData)) {
                $isCheck = false;
                $message = '此分类下还有文章';
            }
        }

        if ($isCheck == false) {
            $message = isset($message) ? $message : '删除失败';
            $this->redirect('article_cat_list', false);
            Util::showMessage($message);
        } else {
            $where = array('id' => $id);
            $result = $catObj->delete($where);
            $result = $catObj->delete();
                    
            $this->redirect('article_cat_list');
        }
    }
    //商品详情
    public function home1(){
        $id = IFilter::act(IReq::get('id', 'post'), 'int');
        $info = M('article')->where(array('id'=>$id))->find();
        //将字符串进行处理
        $info['content']=stripslashes($info['content']);
        $info['content']=stripslashes($info['content']);
        if($info['content']){
            $data = array(
                'status' => '1',
                'data' => $info['content']
            );
        }else{
            $data = array(
                'status' => '0'
            );
        }
        echo json_encode($data);
    }
}
